package com.sgcc.operate.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.operate.service.IOActiTemplateService;
import com.sgcc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.operate.mapper.OActiApplyMapper;
import com.sgcc.operate.domain.OActiApply;
import com.sgcc.operate.service.IOActiApplyService;

/**
 * 活动模板应用Service业务层处理
 *
 * @author sgcc
 * @date 2021-03-31
 */
@Service
public class OActiApplyServiceImpl implements IOActiApplyService {
    @Autowired
    private OActiApplyMapper oActiApplyMapper;
    @Autowired
    private IOActiTemplateService ioActiTemplateService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询活动模板应用
     *
     * @param id 活动模板应用ID
     * @return 活动模板应用
     */
    @Override
    public OActiApply selectOActiApplyById(String id) {
        return oActiApplyMapper.selectOActiApplyById(id);
    }

    /**
     * 查询活动模板应用列表
     *
     * @param oActiApply 活动模板应用
     * @return 活动模板应用
     */
    @Override
    public List<OActiApply> selectOActiApplyList(OActiApply oActiApply) {
        return oActiApplyMapper.selectOActiApplyList(oActiApply);
    }

    /**
     * 新增活动模板应用
     *
     * @param oActiApply 活动模板应用
     * @return 结果
     */
    @Override
    public int insertOActiApply(OActiApply oActiApply) {
        // 判断是否存在预览页面，如果有，删除
//        String path = request.getServletContext().getRealPath("/webpage/modules/oschemeforn/preview/temp/" + oActiApply.getTempId() + "/" + oActiApply.getActiId());
//        File rootDir = new File(path);
//        if (rootDir.exists()) {
//            FileUtil.deleteDir(rootDir);
//        }
        // 执行保存操作
//        Map<String, Object> result = ioActiTemplateService.preview(oActiApply);
        // 查看是否有值
        OActiApply existApply = oActiApplyMapper.queryApplyActi(oActiApply.getTempId(), oActiApply.getActiId());
        if (null != existApply) {
            oActiApply.setUpdateDate(new Date());
            oActiApply.setId(existApply.getId());
            oActiApplyMapper.updateOActiApply(oActiApply);
        } else {
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            LoginUser loginUser = tokenService.getLoginUser();
            oActiApply.setCreateDate(new Date());
            oActiApply.setUpdateDate(new Date());
            oActiApply.setCreateBy(loginUser.getUserid());
            oActiApply.setUpdateBy(loginUser.getUserid());
            oActiApply.setId(id);
            oActiApply.setDelFlag("0");
            oActiApplyMapper.insertOActiApply(oActiApply);
        }
//        oActiApply.setH5Link(result.get("url").toString());
//        super.save(oActiApply);
//        // 删除图片配置
//        actiPicConfigMapper.deleteBatch(oActiApply.getId());
//        List<OActiPicConfig> configs = (List<OActiPicConfig>) result.get("pics");
//        for (OActiPicConfig config : configs) {
//            config.setApplyId(oActiApply.getId());
//            actiPicConfigMapper.insert(config);
//        }

        return 1;
    }

    /**
     * 修改活动模板应用
     *
     * @param oActiApply 活动模板应用
     * @return 结果
     */
    @Override
    public int updateOActiApply(OActiApply oActiApply) {
        return oActiApplyMapper.updateOActiApply(oActiApply);
    }

    /**
     * 批量删除活动模板应用
     *
     * @param ids 需要删除的活动模板应用ID
     * @return 结果
     */
    @Override
    public int deleteOActiApplyByIds(String[] ids) {
        return oActiApplyMapper.deleteOActiApplyByIds(ids);
    }

    /**
     * 删除活动模板应用信息
     *
     * @param id 活动模板应用ID
     * @return 结果
     */
    @Override
    public int deleteOActiApplyById(String id) {
        return oActiApplyMapper.deleteOActiApplyById(id);
    }

    @Override
    public OActiApply queryApplyActi(String tempId, String actiId) {
        return oActiApplyMapper.queryApplyActi(tempId, actiId);
    }

    @Override
    public OActiApply selectOActiApplyByTempIdAndActiId(String tempId, String actiId) {
        OActiApply oActiApply = oActiApplyMapper.queryApplyActi(tempId, actiId);
        JSONArray jsonArray = new JSONArray();
        if(oActiApply!=null&&oActiApply.getActivityUrl()!=null){
            jsonArray=JSONArray.parseArray(oActiApply.getActivityUrl());
        }
        oActiApply.setActivityUrls(jsonArray);
        return oActiApply;
    }
}
